﻿using System;
using System.Linq;
using System.Web.Mvc;
using Lbb.Localization;
using Lbb.Web.Mvc;
using Lbb.URating.Controllers;
using Lbb.URating.DL;
using Lbb.URating.Localization;
using Lbb.URating.Models;

namespace Lbb.URating.Areas.admin.Controllers
{
    /// <summary>
    /// 评分分类控制器
    /// </summary>
    /// <author>Chiron</author>
    public class U_ClickCategoryController : URatingControllerBase
    {
        #region Fields
        private const string _preKey = "U_ClickCategory_";
        private readonly static string[] _listKeys = URatingControllerBase.listKeys.Union(new[] { 
            "objTypes", "allowRedo" 
        }).ToArray();
        #endregion Fields

        #region Pages
        /// <summary>
        /// 列表页面
        /// </summary>
        public ActionResult Index(int? pageNumber, int? pageSize, string keyword, string orderby, string objTypes, bool? allowRedo)
        {
            Response.Expires = 0;
            CacheArgs(_preKey, _listKeys);
            var model = new U_ClickCategoryDataList();
            model.FillData(U_ClickCategoryRepository.Find(model.Keyword, model.ObjectTypes, model.AllowRedo));
            return View(IsRequestPartial ? "List" : "Index", model);
        }
        /// <summary>
        /// 返回到上次查看的列表页
        /// </summary>
        public ActionResult RedirectToIndex()
        {
            return RedirectToAction("Index", GetCacheArgs(_preKey, _listKeys));
        }
        /// <summary>
        /// 添加/编辑页面
        /// </summary>
        public ActionResult Edit(Guid? id, string objectType, bool? allowRedo)
        {
            var model = U_ClickCategoryRepository.Get(id) ?? new U_ClickCategory
            {
                ObjectType = objectType,
                AllowRedo = allowRedo ?? false
            };
            return View(model);
        }
        /// <summary>
        /// 保存评分分类
        /// </summary>
        [HttpPost]
        public ActionResult Edit(U_ClickCategory model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (model.CategoryID == Guid.Empty)
                    {
                        U_ClickCategoryRepository.Add(model);
                    }
                    else
                    {
                        U_ClickCategoryRepository.Update(model);
                    }
                    U_ClickCategoryRepository.Save();
                    return RedirectToIndex();
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }
            }
            return View(model);
        }
        /// <summary>
        /// 删除评分分类
        /// </summary>
        [HttpPost]
        public ActionResult Delete(Guid[] id)
        {
            var result = new JsonResponse();

            if (IsValidValue(ref id))
            {
                try
                {
                    U_ClickCategoryRepository.DeleteRange(U_ClickCategoryRepository.Find(d => id.Contains(d.CategoryID)));
                    U_ClickCategoryRepository.Save();
                }
                catch (Exception ex)
                {
                    result.AppendMessage(ex);
                }
            }
            else
            {
                result.AppendMessage(string.Format(OperatorRes.PleaseSelectObj, OperatorRes.Delete, U_ClickCategoryRes.ClassName));
            }

            result.success = string.IsNullOrEmpty(result.message);
            return Json(result);
        }
        #endregion Pages


    }
}
